﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using TCKQKL.Service.LookupClient;
using TCKQKL.FrontEnd.DataModel;
using TCKQKL.FrontEnd.Config;

namespace TCKQKL.FrontEnd.Web.Controls
{
    public partial class PersonalTradingResultLookup : System.Web.UI.UserControl
    {
        public List<InvestorDataUI> assigns;
        protected void Page_Load(object sender, EventArgs e)
        {
            string HUMAN_QUERY = "<b>Tìm các lệnh: </b>";
            #region CheckAuthentication
            string userid = HttpContext.Current.User.Identity.Name;
            if (userid == "")
            {
                HttpCookie authcookie = Request.Cookies[FormsAuthentication.FormsCookieName];
                if (authcookie == null)
                {
                    FormsAuthentication.SignOut();
                    Response.Redirect(Settings.URL_PAGE_LOGIN);
                }
                FormsAuthenticationTicket ticket = (FormsAuthenticationTicket)FormsAuthentication.Decrypt(authcookie.Value);
                userid = ticket.Name;
            }
            else
            {
                LSClient client = new LSClient();
                assigns = client.getAssignAccountsByInvestorId(client.getAccountData(HttpContext.Current.User.Identity.Name).investorId);
                client.Close();
            }

            #endregion
            #region Params
            DateTime fromDate = DateTime.Now;
            DateTime toDate = DateTime.Now;
            string orderType = "MATCHED";
            string orderClass = "";
            string orderSide = "";
            string symbol = "";
            string ID = "";
            decimal priceLow = 0;
            decimal priceHigh = 0;
            decimal qttyLow = 0;
            decimal qttyHigh = 0;
            bool assignAccount = false;
            try
            {
                if (IsPostBack)
                {
                    assignAccount = chkAssigns.Checked;
                    HUMAN_QUERY += "<br />Tài khoản: " + ((assignAccount) ? "Cả TK uỷ quyền" : "Tài khoản chính");
                    switch (ddlDate.SelectedValue)
                    {
                        case "ALL":
                            fromDate = Convert.ToDateTime("1/1/2007");
                            toDate = DateTime.Today;
                            break;
                        case "TODAY":
                            fromDate = DateTime.Today;
                            toDate = DateTime.Today;
                            break;
                        case "THISWEEK":
                            fromDate = DateTime.Today.AddDays(0 - DateTime.Today.DayOfWeek);
                            toDate = DateTime.Today;
                            break;
                        case "THISMONTH":
                            fromDate = DateTime.Today.AddDays(0 - DateTime.Today.Day);
                            toDate = DateTime.Today;
                            break;
                        case "SPECIFIC":
                            fromDate = Convert.ToDateTime(this.date1.Text);
                            toDate = Convert.ToDateTime(this.date2.Text);
                            break;
                        default:
                            break;
                    }
                    HUMAN_QUERY += "<br />Từ: " + fromDate.ToString("dd/MM")
                        + " đến " + toDate.ToString("dd/MM");
                    orderType = lstOrderType.SelectedValue;
                    HUMAN_QUERY += "<br />Kiểu: " + lstOrderType.SelectedItem.Text;
                    orderClass = lstOrderClass.SelectedValue;
                    HUMAN_QUERY += "<br />Loại: " + lstOrderClass.SelectedItem.Text;
                    orderSide = lstOrderSide.SelectedValue;
                    HUMAN_QUERY += "<br />Mua/Bán: " + lstOrderSide.SelectedItem.Text;
                    symbol = this.txtSymbol.Text.Trim();
                    HUMAN_QUERY += "<br />MCK: " + ((txtSymbol.Text.Trim().Length == 0) ? "Tất cả" : txtSymbol.Text.Trim());
                    ID = this.txtOrderID.Text.Trim();
                    HUMAN_QUERY += "<br />ID: " + ((txtSymbol.Text.Trim().Length == 0) ? "Tất cả" : txtOrderID.Text.Trim());
                    switch (ddlOrderPrice.SelectedValue)
                    {
                        case "SMALL":
                            priceLow = 0;
                            priceHigh = 30000;
                            break;
                        case "MEDIUM":
                            priceLow = 30001;
                            priceHigh = 69999;
                            break;
                        case "HIGH":
                            priceLow = 70000;
                            priceHigh = 99999999;
                            break;
                        case "SPECIFIC":
                            switch (lstPriceConstrain.SelectedValue)
                            {
                                case "ATLEAST":
                                    priceLow = Convert.ToDecimal(txtPrice.Text);
                                    priceHigh = 999999999;
                                    break;
                                case "ATMOST":
                                    priceLow = 0;
                                    priceHigh = Convert.ToDecimal(txtPrice.Text);
                                    break;
                                case "EXACT":
                                    priceLow = priceHigh = Convert.ToDecimal(txtPrice.Text);
                                    break;
                            }
                            break;
                        default:
                            break;
                    }
                    HUMAN_QUERY += "<br />Mức giá: " + (((priceLow == 0) && (priceHigh == 0)) ? "Tất cả" : priceLow.ToString() + "-" + priceHigh.ToString());
                    switch (ddlOrderQtty.SelectedValue)
                    {
                        case "SMALL":
                            qttyLow = 0;
                            qttyHigh = 500;
                            break;
                        case "MEDIUM":
                            qttyLow = 501;
                            qttyHigh = 1999;
                            break;
                        case "HIGH":
                            qttyLow = 2000;
                            qttyHigh = 99999999;
                            break;
                        case "SPECIFIC":
                            switch (lstQttyConstrain.SelectedValue)
                            {
                                case "ATLEAST":
                                    qttyLow = Convert.ToDecimal(txtQtty.Text);
                                    qttyHigh = 999999999;
                                    break;
                                case "ATMOST":
                                    qttyLow = 0;
                                    qttyHigh = Convert.ToDecimal(txtQtty.Text);
                                    break;
                                case "EXACT":
                                    qttyLow = qttyHigh = Convert.ToDecimal(txtQtty.Text);
                                    break;
                            }
                            break;
                        default:
                            break;
                    }
                    HUMAN_QUERY += "<br />Khối lượng: " + (((qttyLow == 0) && (qttyHigh == 0)) ? "Tất cả" : qttyLow.ToString() + "-" + qttyHigh.ToString());
                    this.lblTextQuery.Text = HUMAN_QUERY;
                }
            }
            catch
            {
                lblTextQuery.Text = Settings.ERROR_INPUT_PARAMS;
            }
            #endregion Paramss
            if (IsPostBack)
            {
                LSClient client = new LSClient();
                this.PersonalTradingResultShort1.personal = client.getPersonalTradingResultHistory(client.getAccountData(HttpContext.Current.User.Identity.Name).investorId, fromDate, toDate, orderType, orderClass, orderSide, symbol, ID, priceLow, priceHigh, qttyLow, qttyHigh, assignAccount);
                client.Close();
            }
        }
    }
}